package com.wang.yygh.controller.api;

import com.wang.yygh.common.exception.YyghException;
import com.wang.yygh.common.helper.HttpRequestHelper;
import com.wang.yygh.common.result.Result;
import com.wang.yygh.common.result.ResultCodeEnum;
import com.wang.yygh.common.utils.MD5;
import com.wang.yygh.model.hosp.Department;
import com.wang.yygh.model.hosp.Hospital;
import com.wang.yygh.model.hosp.Schedule;
import com.wang.yygh.service.DepartmentService;
import com.wang.yygh.service.HospitalService;
import com.wang.yygh.service.HospitalSetService;
import com.wang.yygh.service.ScheduleService;
import com.wang.yygh.vo.hosp.DepartmentQueryVo;
import com.wang.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
 *  1.获取传递过来的医院信息
 *  2.根据传递过来的医院信息查询数据库，查询出签名
 *  3.比对传递过来的签名与从数据库中查询出来的签名
 *  4.相等则继续，不等则抛出异常
 */
@RestController
@RequestMapping("/api/hosp")
public class ApiController  {
    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    //查询医院接口
    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request){
        //获取接口处传来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //将传过来的Map<String, String[]> 转化为 Map<String, Object> ，String[] -> Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号
        String hoscode =(String) paramMap.get("hoscode");

        //1.获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String)paramMap.get("sign");

        //2.根据传递过来的医院编码，查询数据库中该医院的签名
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3.把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //4.判断两者签名是否一样
        if (!hospSign.equals(signKeyMd5)){
            throw  new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法实现根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return  Result.ok(hospital);
    }


    //上传医院接口
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request){
        //获取接口处传来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //将传过来的Map<String, String[]> 转化为 Map<String, Object> ，String[] -> Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //1.获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String)paramMap.get("sign");

        //2.根据传递过来的医院编码，查询数据库中该医院的签名
        String hoscode = (String)paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3.把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //4.判断两者签名是否一样
        if (!hospSign.equals(signKeyMd5)){
            throw  new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //传输过程中 "+"  转变为" "   ，因此需要转换回来才能显示图片
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        paramMap.put("logoData",logoData);

        //调用service方法
        hospitalService.save(paramMap);
       return Result.ok();
    }

    //上传科室接口
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取接口处传来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //将传过来的Map<String, String[]> 转化为 Map<String, Object> ，String[] -> Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //1.获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String)paramMap.get("sign");

        //2.根据传递过来的医院编码，查询数据库中该医院的签名
        String hoscode = (String)paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3.把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //4.判断两者签名是否一样
        if (!hospSign.equals(signKeyMd5)){
            throw  new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法
        departmentService.save(paramMap);
        return Result.ok();
    }

    //查询科室接口
    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request){
        //获取接口处传来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //将传过来的Map<String, String[]> 转化为 Map<String, Object> ，String[] -> Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //医院编号
        String hoscode =(String) paramMap.get("hoscode");
        //当前页、每页的记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));

        //签名校验
        //1.获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String)paramMap.get("sign");
        //2.根据传递过来的医院编码，查询数据库中该医院的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
        //4.判断两者签名是否一样
        if (!hospSign.equals(signKeyMd5)){
            throw  new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //构造出专门用于查询的科室类
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用service方法
        Page<Department> pageModel = departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageModel);
    }

    //删除科室接口
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //获取接口处传来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //将传过来的Map<String, String[]> 转化为 Map<String, Object> ，String[] -> Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //医院编号 科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");

        //签名校验
        //1.获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String)paramMap.get("sign");
        //2.根据传递过来的医院编码，查询数据库中该医院的签名
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3.把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //4.判断两者签名是否一样
        if (!hospSign.equals(signKeyMd5)){
            throw  new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.remove(hoscode,depcode);
        return  Result.ok();
    }

    //上传排班接口
    @PostMapping("/saveSchedule")
    public Result saveSchedule (HttpServletRequest request){
        //获取接口处传来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //将传过来的Map<String, String[]> 转化为 Map<String, Object> ，String[] -> Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //签名校验
        String hospSign = (String)paramMap.get("sign");
        String hoscode =(String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);

        //4.判断两者签名是否一样
        if (!hospSign.equals(signKeyMd5)){
            throw  new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.save(paramMap);
        return  Result.ok();
    }

    //查询排班接口
    @PostMapping("/schedule/list")
    public Result findSchedule(HttpServletRequest request){
        //获取接口处传来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //将传过来的Map<String, String[]> 转化为 Map<String, Object> ，String[] -> Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //医院，科室编号
        String hoscode =(String) paramMap.get("hoscode");
        //String depcode = (String) paramMap.get("depcode");

        //当前页、每页的记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));

        //签名校验
        //1.获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String)paramMap.get("sign");
        //2.根据传递过来的医院编码，查询数据库中该医院的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
        //4.判断两者签名是否一样
        if (!hospSign.equals(signKeyMd5)){
            throw  new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //构造出专门用于查询的排班类
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        //scheduleQueryVo.setDepcode(depcode);
        //调用service方法
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);
    }

    //删除排班接口
    @PostMapping("/schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        //获取接口处传来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //将传过来的Map<String, String[]> 转化为 Map<String, Object> ，String[] -> Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //医院编号 排班编号
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");

        //签名校验
        //1.获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String)paramMap.get("sign");
        //2.根据传递过来的医院编码，查询数据库中该医院的签名
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3.把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //4.判断两者签名是否一样
        if (!hospSign.equals(signKeyMd5)){
            throw  new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.remove(hoscode,hosScheduleId);
        return  Result.ok();
    }
}
